package ex.recruit.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.recruit.entity.ApplyDivide;
import modules.recruit.form.ApplyDivideForm;
import modules.recruit.service.ApplyDivideService;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.guinea.core.PropertyFilterForSql;
import com.guinea.core.orm.Page;
import com.guinea.core.orm.PropertyFilter;
import com.guinea.core.service.EntityService;
import com.guinea.core.utils.ServletUtils;

import ex.system.service.UserServiceEx;

public class ApplyDivideActionEx extends CommonApplyJudgment {

	UserServiceEx userServiceEx;

	ApplyDivideService applyDivideService;

	public EntityService getEntityService() {
		return applyDivideService;
	}

	public ApplyDivideService getApplyDivideService() {
		return applyDivideService;
	}

	public void setApplyDivideService(ApplyDivideService applyDivideService) {
		this.applyDivideService = applyDivideService;
	}

	public UserServiceEx getUserServiceEx() {
		return userServiceEx;
	}

	public void setUserServiceEx(UserServiceEx userServiceEx) {
		this.userServiceEx = userServiceEx;
	}

	/**
	 * 初始化业务对象的函数.
	 */
	protected void findEntity(ActionForm form, HttpServletRequest request) {
		super.findEntity(form, request);
		if (entity == null && applyBase != null) {
			entity = applyBase.getDivide();
		}
	}

	/**
	 * 列出所有对象的Action函数.
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 从request中获取含属性前缀名的参数,构造去除前缀名后的参数Map.
		Map filterParamMap = ServletUtils.getParametersStartingWith(request,
				FILTER_PREFIX + "_");
		List filters = PropertyFilterForSql.buildFromParaMap(filterParamMap);
		
		filters.add(new PropertyFilterForSql("step", "2"));
		Page page = PropertyFilter.buildPageFromHttpRequest(request,
				REQUEST_PAGE);
		
		page = getApplyBaseServiceEx().getApplyProcess(page, filters);
		request.setAttribute("regionManagers", getUserServiceEx()
				.getUsersByRoleName("role_recruit_region_manager"));

		request.setAttribute(REQUEST_PAGE, page);
		request.setAttribute(FILTER_MAP, filterParamMap);
		return mapping.findForward(LIST);
	}

	/**
	 * 显示修改对象Form的Action函数.
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		bindForm(form, entity);
		ApplyDivideForm applyDivideForm = (ApplyDivideForm) form;
		applyDivideForm.setApplyBaseId(applyBaseId);

		return handleEdit(mapping, form, request, response);
	}
	
	protected void doInitEntity(HttpServletRequest request, ActionForm form) {
		super.doInitEntity(request, form);
		if (isCreateOperation) {
			ApplyDivide applyDivide = (ApplyDivide) entity;
			applyDivide.setBase(applyBase);
			entity = applyDivide;
		}
	}
	/**
	 * 保存对象的Action函数.
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		doInitEntity(request, form);

		doSaveEntity(request, form);
		
		request.setAttribute("regionManagers", getUserServiceEx()
				.getUsersByRoleName("role_recruit_region_manager"));
		return mapping.findForward(LIST);
	}

	protected ActionForward handleEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		List m = getUserServiceEx()
		.getUsersByRoleName("role_recruit_region_manager");
		request.setAttribute("regionManagers", m);
		return super.handleEdit(mapping, form, request, response);
	}

}
